package zhoufu.sz.sanxiao_check.ui.basic;

import android.databinding.ViewDataBinding;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import java.util.List;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.ActivityBasicItem3Binding;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.IDCardModel;
import zhoufu.sz.sanxiao_check.presents.BasicItem3Presenter;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/26
 * 资料上传
 */
public class BasicItem3Activity extends BaseMsgActivity implements View.OnClickListener,
        BasicMsgItem2Contract.View<IDCardModel>
{
    @Inject
    BasicItem3Presenter _presenter;

    ActivityBasicItem3Binding _binding;

    private FunctionConfig config;


    @Override protected void initInject()
    {
        config = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setMutiSelectMaxSize(1)
                .build();
        setTitle(R.string.title_basic_item3);
        LoginComponet loginComponet = DaggerLoginComponet.builder()
                .activityModule(getActivityModule()).appComponent(getAppComponent()).build();
        loginComponet.inject(this);
        _presenter.bidnView(this);
        _presenter.onStart();
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        super.initAfterOnCreate(binding);
        _binding = (ActivityBasicItem3Binding) binding;
        _binding.setOnClickListener(this);
    }

    @Nullable @Override protected android.databinding.DataBindingComponent setBindingComponent()
    {
        return LoginComponet.Instance.get();
    }

    @Override protected int setIndividualId()
    {
        return R.layout.activity_basic_item3;
    }


    @Override public void onClick(View v)
    {

        switch (v.getId())
        {
            case R.id.mIvCardID:
                GalleryFinal.openGalleryMuti(0, config, mResultCallback);
                break;
            case R.id.mItem3ID:
                _navigator.startShowPic(this, _binding.getModel().License.get());
                break;
            case R.id.mIDCard1:
                _navigator.startShowPic(this, _binding.getModel().CardTo.get());
                break;
            case R.id.mIvCardTo:
                GalleryFinal.openGalleryMuti(1, config, mResultCallback);
                break;
            case R.id.mIDCard2:
                _navigator.startShowPic(this, _binding.getModel().CardEnd.get());
                break;
            case R.id.mIvCardEnd:
                GalleryFinal.openGalleryMuti(2, config, mResultCallback);
                break;
        }
    }

    @Override public String SID()
    {
        return mInfoModel.getSID();
    }

    @Override public void setModel(IDCardModel fireNoticeModel)
    {
        _binding.setModel(fireNoticeModel);
    }

    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        if ( item.getItemId() == R.id.menu_save )
        {
            _presenter.update();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override public void updateSuccese()
    {
        UIUtils.showToast(this, "保存成功");
        finish();
    }

    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(_binding.getRoot(), msg);
    }

    @Override public void pullFaild()
    {
        UIUtils.showSnackbar(_binding.getRoot(), R.string.toast_network_error);
    }

    @Override public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override public void pullStart()
    {
        showProgreesDialog();
    }

    @Override protected void onDestroy()
    {
        super.onDestroy();
        _presenter.onDestroy();
    }

    private GalleryFinal.OnHanlderResultCallback mResultCallback
            = new GalleryFinal.OnHanlderResultCallback()
    {
        @Override public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList)
        {
            if ( resultList != null && resultList.size() > 0 )
            {
                PhotoInfo info = resultList.get(0);
                switch (reqeustCode)
                {
                    case 0:
                        _presenter.get_model().License.set(info.getPhotoPath());
                        break;
                    case 1:
                        _presenter.get_model().CardTo.set(info.getPhotoPath());
                        break;
                    case 2:
                        _presenter.get_model().CardEnd.set(info.getPhotoPath());
                        break;
                }
            }
        }

        @Override public void onHanlderFailure(int requestCode, String errorMsg)
        {
            UIUtils.showSnackbar(_binding.getRoot(), errorMsg);
        }
    };
}
